package com.hspedu.copyfile;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/2
 * @time 18:31
 */
public class TCPFileUploadClient {
    public static void main(String[] args) throws IOException {
        //建立连接
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);

        //读取图片
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\资料\\分享资料\\hnt.png"));
        byte[] buffer = new byte[1024];
        int readLength = 0;

        //创建输出流
        OutputStream outputStream = socket.getOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        while ((readLength = bufferedInputStream.read(buffer)) != -1){
            bufferedOutputStream.write(buffer,0,readLength);
        }
        bufferedOutputStream.flush();
        socket.shutdownOutput();

        //收取服务端回传的信息
        byte[] byte2 = new byte[512];
        int readLen = 0;
        InputStream inputStream = socket.getInputStream();
        BufferedInputStream bufferedInputStream1 = new BufferedInputStream(inputStream);
        while ((readLen = bufferedInputStream1.read(byte2)) != -1){
            System.out.println(new String(byte2, 0, readLen));
        }

        socket.close();

    }
}
